package org.edu.modules.sxjy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.VeBaseTeacher;
import org.edu.modules.sxjy.entity.VeClassTeacher;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VeDepartment;
import org.edu.modules.sxjy.entity.VeTeacherlog;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VeTeacherLogMapper;
import org.edu.modules.sxjy.service.IVeClassTeacharService;
import org.edu.modules.sxjy.service.IVeTeacherLogService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeTeacherlogAddVo;
import org.edu.modules.sxjy.vo.VeTeacherlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeTeacherLogServiceImpl
        extends ServiceImpl<VeTeacherLogMapper, VeTeacherlog>
        implements IVeTeacherLogService
{
    @Autowired
    private VeTeacherLogMapper veTeacherLogMapper;
    @Autowired
    private IVeTeacherLogService veTeacherLogService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private IVeClassTeacharService veClassTeacharService;

    public List<VeTeacherlogVo> selectAll(VeTeacherlogVo veTeacherlogVo, Integer startLine, Integer pageSize, VeCommon veCommon)
    {
        List<VeTeacherlogVo> veTeacherlogVos = new ArrayList();
        Integer userIds;
        if (veCommon.getUserType().contains("0"))
        {
            veTeacherlogVos = this.veTeacherLogMapper.selectAll(veTeacherlogVo, startLine, pageSize, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                veTeacherlogVos = this.veTeacherLogMapper.selectAll(veTeacherlogVo, startLine, pageSize, veCommon);
            } else {
                veTeacherlogVos = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                veTeacherlogVos = this.veTeacherLogMapper.selectAll(veTeacherlogVo, startLine, pageSize, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            userIds = this.veTeacherLogMapper.getCpIdByUserId(veCommon.getUserId());
            veCommon.setUserIds(userIds.intValue());
            veTeacherlogVos = this.veTeacherLogMapper.selectAll(veTeacherlogVo, startLine, pageSize, veCommon);
        }
        if ((veTeacherlogVos != null) && (veTeacherlogVos.size() > 0)) {
            //lbh 2024/07/31 userIds >> Iterator veTeacherlogVoIterator ,for循环内所有userIds >> veTeacherlogVoIterator
            for (Iterator veTeacherlogVoIterator = veTeacherlogVos.iterator(); veTeacherlogVoIterator.hasNext();)
            {
                //lbh 2024/07/31 teacherlogVo >> VeTeacherlogVo teacherlogVo
                VeTeacherlogVo teacherlogVo = (VeTeacherlogVo)veTeacherlogVoIterator.next();
                teacherlogVo.setStartTime(DateUtils.getDateToString(Long.parseLong(teacherlogVo.getStartTime())));
                teacherlogVo.setEndTime(DateUtils.getDateToString(Long.parseLong(teacherlogVo.getEndTime())));
                teacherlogVo.setCreateTime(DateUtils.getDateToString(Long.parseLong(teacherlogVo.getCreateTime())));

                Object linkWayResult = this.veCommonClient.queryDictDataByModelCode("09d5e1e7f9b049008eee645c783a1d67", "LINKWAY", veTeacherlogVo.getLinkWay());
                if (((Result)linkWayResult).getCode().intValue() == 200)
                {
                    List<Object> linkwayResultList = (List)((Result)linkWayResult).getResult();
                    for (Object o : linkwayResultList)
                    {
                        Map linkwayMap = (Map)o;
                        if ((linkwayMap != null) &&
                                (linkwayMap.get("code").equals(teacherlogVo.getLinkWay()))) {
                            teacherlogVo.setLinkWay(linkwayMap.get("title").toString());
                        }
                    }
                }
            }
        }
        VeTeacherlogVo teacherlogVo;
        return veTeacherlogVos;
    }

    public int getSumPage(VeTeacherlogVo veTeacherlogVo, VeCommon veCommon)
    {
        return this.veTeacherLogMapper.getSumPage(veTeacherlogVo, veCommon);
    }

    public VeTeacherlogVo getOneInfo(int id)
    {
        VeTeacherlog veTeacherlog = (VeTeacherlog)this.veTeacherLogService.getById(Integer.valueOf(id));

        VeTeacherlogVo veTeacherlogVo = new VeTeacherlogVo();

        BeanUtils.copyProperties(veTeacherlog, veTeacherlogVo);


        veTeacherlogVo.setCreateTime(DateUtils.getDateToString(veTeacherlog.getCreateTime()));


        veTeacherlogVo.setStartTime(DateUtils.getDateToString(veTeacherlog.getStartTime()));

        Result<?> linkWayResult = this.veCommonClient.queryDictDataByModelCode("09d5e1e7f9b049008eee645c783a1d67", "LINKWAY", veTeacherlog.getLinkWay());
        if (linkWayResult.getCode().intValue() == 200)
        {
            List<Object> linkwayResultList = (List)linkWayResult.getResult();
            for (Object o : linkwayResultList)
            {
                Map linkwayMap = (Map)o;
                if ((linkwayMap != null) &&
                        (linkwayMap.get("code").equals(veTeacherlogVo.getLinkWay()))) {
                    veTeacherlogVo.setLinkWay(linkwayMap.get("title").toString());
                }
            }
        }
        return veTeacherlogVo;
    }

    public void saves(VeTeacherlogAddVo veTeacherlogAddVo)
    {
        VeTeacherlog veTeacherlog = new VeTeacherlog();



        veTeacherlog.setTitle(veTeacherlogAddVo.getTitle());

        veTeacherlog.setStartTime(DateUtils.getStringToDate(veTeacherlogAddVo.getStartTime()));

        veTeacherlog.setEndTime(DateUtils.getStringToDate(veTeacherlogAddVo.getEndTime()));

        veTeacherlog.setContent(veTeacherlogAddVo.getContent());

        veTeacherlog.setLinkWay(veTeacherlogAddVo.getLinkWay());
        if (veTeacherlogAddVo.getUserType().equals("1"))
        {
            Object TeacharResult = this.veCommonClient.queryTeacherByUserId("09d5e1e7f9b049008eee645c783a1d67", veTeacherlogAddVo.getUserId()).getResult();
            Map teacherMap = (Map)TeacharResult;
            String s = JSON.toJSONString(teacherMap);
            VeBaseTeacher veBaseTeacher = (VeBaseTeacher)JSON.parseObject(s, VeBaseTeacher.class);


            Integer id = veBaseTeacher.getId();
            if ((id != null) && (!"".equals(id))) {
                veTeacherlog.setUserId(id.intValue());
            }
            Integer depId = veBaseTeacher.getDepId();
            if ((depId != null) && (!"".equals(depId))) {
                veTeacherlog.setDepId(depId);
            }
            Integer jyzId = veBaseTeacher.getJyzId();
            if ((jyzId != null) && (!"".equals(jyzId))) {
                veTeacherlog.setJyzId(jyzId);
            }
            String xm = veBaseTeacher.getXm();
            if ((xm != null) && (!"".equals(xm))) {
                veTeacherlog.setXm(xm);
            }
            String gh = veBaseTeacher.getGh();
            if ((gh != null) && (!"".equals(gh))) {
                veTeacherlog.setGh(gh);
            }
            veTeacherlog.setCreateTime(System.currentTimeMillis());

            veTeacherlog.setTerminalId(1);
        }
        else if (veTeacherlogAddVo.getUserType().equals("4"))
        {
            VeCompanyUser veCompanyUser = this.veTeacherLogMapper.getCpUserIdByUserId(veTeacherlogAddVo.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                Integer id = Integer.valueOf(veCompanyUser.getId());
                if ((id != null) && (!"".equals(id))) {
                    veTeacherlog.setUserId(id.intValue());
                }
                String xm = veCompanyUser.getRealName();
                if ((xm != null) && (!"".equals(xm))) {
                    veTeacherlog.setXm(xm);
                }
                String gh = veCompanyUser.getAccount();
                if ((gh != null) && (!"".equals(gh))) {
                    veTeacherlog.setGh(gh);
                }
                veTeacherlog.setCreateTime(System.currentTimeMillis());

                veTeacherlog.setTerminalId(1);
            }
        }
        this.veTeacherLogService.save(veTeacherlog);
    }

    public VeTeacherlogVo editEcho(int id)
    {
        VeTeacherlogVo veTeacherlogVo = new VeTeacherlogVo();

        VeTeacherlog veTeacherlog = (VeTeacherlog)this.veTeacherLogService.getById(Integer.valueOf(id));
        if ((veTeacherlog != null) && (!"".equals(veTeacherlog)))
        {
            veTeacherlogVo.setId(veTeacherlog.getId());

            veTeacherlogVo.setTitle(veTeacherlog.getTitle());

            veTeacherlogVo.setStartTime(DateUtils.getDateToString(veTeacherlog.getStartTime()));

            veTeacherlogVo.setEndTime(DateUtils.getDateToString(veTeacherlog.getEndTime()));

            veTeacherlogVo.setContent(veTeacherlog.getContent());

            veTeacherlogVo.setLinkWay(veTeacherlog.getLinkWay());
        }
        return veTeacherlogVo;
    }

    public void edit(VeTeacherlogVo veTeacherlogVo)
    {
        VeTeacherlog veTeacherlog = (VeTeacherlog)this.veTeacherLogService.getById(Integer.valueOf(veTeacherlogVo.getId()));
        if ((veTeacherlog != null) && (!"".equals(veTeacherlog)))
        {
            veTeacherlog.setId(veTeacherlogVo.getId());

            veTeacherlog.setTitle(veTeacherlogVo.getTitle());

            veTeacherlog.setStartTime(DateUtils.getStringToDate(veTeacherlogVo.getStartTime()));

            veTeacherlog.setEndTime(DateUtils.getStringToDate(veTeacherlogVo.getEndTime()));

            veTeacherlog.setContent(veTeacherlogVo.getContent());

            veTeacherlog.setLinkWay(veTeacherlogVo.getLinkWay());
        }
        this.veTeacherLogService.updateById(veTeacherlog);
    }

    public List<VeDepartment> getSectionInfo()
    {
        return this.veTeacherLogMapper.getSectionInfo();
    }
}
